#ifndef __TRADE_DATA_H_INCLUDE__
#define __TRADE_DATA_H_INCLUDE__


#define ORDERREF m_SessionInfo.MaxOrderRef

typedef CThostFtdcInstrumentField            INSTRUMENT;
typedef CThostFtdcTradingAccountField           ACCOUNT;
typedef CThostFtdcOrderField                      ORDER;
typedef CThostFtdcTradeField                   CTPTRADE;
typedef CThostFtdcSettlementInfoConfirmField SETTLEMENT;
typedef CThostFtdcInputOrderActionField  CTPRECALLORDER;

typedef ORDER*            OrderArray;
typedef CTPTRADE*         TradeArray;
typedef INSTRUMENT * InstrumentArray;


typedef struct TRADE_POSITION {
	char   m_strTradeID[21];			//TThostFtdcTradeIDType
	char   m_strInstrumentID[31];		//TThostFtdcInstrumentIDType
	char   m_cDirection;				//TThostFtdcDirectionType	THOST_FTDC_D_Buy '0' THOST_FTDC_D_Sell '1'
	int    m_nVolume;
	double m_fOpenPrice;
	time_t m_tOpenTime;					//TThostFtdcDateType	TradeDate; TThostFtdcTimeType	TradeTime;
	char   m_cPriceSource;
	double m_fPointRevenue;
	double m_fBail;
	double m_fTargetPrice;
	time_t m_tOrderTime;
} TRADE_POSITION;

typedef struct INSTMNT_POSITION {
	char   m_strInstrumentID[31];		//TThostFtdcInstrumentIDType
	char   m_cDirection;				//TThostFtdcDirectionType	THOST_FTDC_D_Buy '0' THOST_FTDC_D_Sell '1'
	int    m_nVolume;
	double m_fAveragePrice;
	double m_fPointRevenue;
	double m_fBail;
} INSTMNT_POSITION;

typedef vector<TRADE_POSITION> TradePositions;
typedef vector<INSTMNT_POSITION> InstrPositions;

namespace CTPTDIF {
class CTradeData
{
public:
	CTradeData();
	~CTradeData();

	InstrumentArray GetInstruments(int * nCount) { *nCount = m_nInstruments; return m_pInstruments; }
	OrderArray      GetOrder(int * nCount) { *nCount = m_nOrders; return m_pOrders; }
	TradeArray      GetTrade(int * nCount) { *nCount = m_nTrades; return m_pTrades; }
	InstrPositions* GetInstrPositions() { return &m_vctInsPos; }
	TradePositions* GetTradePositions() { return &m_vctTrdPos; }

	int GetInstrPositionByIndex(int n, INSTMNT_POSITION& ip);
	int GetTradePositionByIndex(int n, TRADE_POSITION& tp);

	ORDER * FindOrder(int nFrontID, int nSessionID, const char * strOrderRef, int * nIndex);
	ORDER * FindOrder(const char * OrderSysId, int * nIndex);
	int     IsSameOrderStatus(ORDER * o1, ORDER * o2);
	ORDER * AddOrder(ORDER * order);
	void    UpdateOrderStatus(ORDER * OldOrder, ORDER * NewOrder);

	virtual void AddToInstrumentPosition(CTPTRADE * ptrade)   = 0;
	virtual void DelFromInstrumentPosition(CTPTRADE * ptrade) = 0;
	INSTMNT_POSITION * FindInsPos(const char * Instrumnet, char cDirection, int * nIndex);

	virtual void CreateTradePos(CTPTRADE * ptrade) = 0;
	virtual void DeleteTradePos(CTPTRADE * ptrade) = 0;
	TRADE_POSITION * FindOpenPos(const char * tradeid);

protected:
	InstrumentArray  m_pInstruments;
	int              m_nInstruments;
	OrderArray            m_pOrders;
	int                   m_nOrders;
	TradeArray            m_pTrades;
	int                   m_nTrades;
	InstrPositions      m_vctInsPos;
	TradePositions      m_vctTrdPos;

	ACCOUNT               m_account;
	SETTLEMENT         m_settlement;

	INSTMNT_POSITION         m_inspos;
protected:
	CriticalLock	          m_mtxTradePos;
	CriticalLock           m_mtxInstrPos;
	//CriticalLock              m_mtxOrder;
};

} //namespace CTPTDIF

#endif //__TRADE_DATA_H_INCLUDE__
